package com.lonely.inventory.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Tuple;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lonely.common.enums.CommonStatus;
import com.lonely.common.exception.BusinessException;
import com.lonely.common.utils.SecurityUtils;
import com.lonely.inventory.domain.dto.surplus.EditSurplusInventorySizeQuantityDto;
import com.lonely.inventory.domain.entity.SysSurplusInventorySizeInfo;
import com.lonely.inventory.mapper.SysSurplusInventorySizeInfoMapper;
import com.lonely.inventory.service.ISurplusInventorySizeInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : 黄志标
 * @date: 2025/9/19 10:05
 * @description: 尾货库存尺码明细服务实现类
 */
@Service
public class SurplusInventorySizeInfoServiceImpl extends ServiceImpl<SysSurplusInventorySizeInfoMapper, SysSurplusInventorySizeInfo> implements ISurplusInventorySizeInfoService {

    /**
     * 更新尾货库存尺码数量
     *
     * @param sizeQuantityDtos
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Tuple> updateSizeQuantityInfo(List<EditSurplusInventorySizeQuantityDto> sizeQuantityDtos) {
        if (CollUtil.isEmpty(sizeQuantityDtos)) {
            throw new BusinessException("没有要更新的尾货库存尺码数据");
        }

        Long surplusInventoryId = sizeQuantityDtos.get(0).getSurplusInventoryId();
        List<String> sizeNames = sizeQuantityDtos.stream().map(EditSurplusInventorySizeQuantityDto::getSizeName).collect(Collectors.toList());
        List<SysSurplusInventorySizeInfo> oldInventorySizeInfos = super.list(new LambdaQueryWrapper<SysSurplusInventorySizeInfo>()
                .eq(SysSurplusInventorySizeInfo::getSurplusInventoryId, surplusInventoryId)
                .in(SysSurplusInventorySizeInfo::getSizeName, sizeNames)
                .eq(SysSurplusInventorySizeInfo::getStatus, CommonStatus.ENABLE.code)
        );
        if (CollUtil.isEmpty(oldInventorySizeInfos)) {
            throw new BusinessException("找不到指定尺寸的尾货库存数据");
        }

        Map<String, Integer> sizeQuantityMap = sizeQuantityDtos.stream().collect(Collectors.toMap(EditSurplusInventorySizeQuantityDto::getSizeName, EditSurplusInventorySizeQuantityDto::getQuantity, (a, b) -> a));
        List<SysSurplusInventorySizeInfo> newInventorySizeInfos = oldInventorySizeInfos.stream().map(x -> {
            SysSurplusInventorySizeInfo newSurplusInventorySizeInfo = new SysSurplusInventorySizeInfo();
            BeanUtils.copyProperties(x, newSurplusInventorySizeInfo);
            newSurplusInventorySizeInfo.setInventoryStockQuantity(MapUtil.getInt(sizeQuantityMap, x.getSizeName(), 0));
            newSurplusInventorySizeInfo.setUpdateBy(SecurityUtils.getUserId());
            return newSurplusInventorySizeInfo;
        }).collect(Collectors.toList());
        super.updateBatchById(newInventorySizeInfos);

        // 返回更新前后对比数据
        Map<String, Integer> oldSizeQuantityMap = oldInventorySizeInfos.stream().collect(Collectors.toMap(SysSurplusInventorySizeInfo::getSizeName, SysSurplusInventorySizeInfo::getInventoryStockQuantity, (a, b) -> a));
        Map<String, Integer> newSizeQuantityMap = newInventorySizeInfos.stream().collect(Collectors.toMap(SysSurplusInventorySizeInfo::getSizeName, SysSurplusInventorySizeInfo::getInventoryStockQuantity, (a, b) -> a));

        Map<String, Tuple> comparisonData = new LinkedHashMap<>();
        sizeNames.forEach(sizeName -> {
            int oldQuantity = MapUtil.getInt(oldSizeQuantityMap, sizeName, 0);
            int newQuantity = MapUtil.getInt(newSizeQuantityMap, sizeName, 0);
            if (oldQuantity != newQuantity) {
                comparisonData.put(sizeName, new Tuple(oldQuantity, newQuantity));
            }
        });
        return comparisonData;

    }
}
